<?php 
	require_once '../function/db_conn.php';
	
	$pbBid = $_GET['id'];
	$pbid = $_GET['pbid'];
	$mid = $_GET['mid'];

	$i = 0;
	
	$pbResult = mysql_query('SELECT * FROM  product_buy WHERE product_buy_id = "' . $pbid . '"');
	$pbRow = mysql_fetch_array($pbResult, MYSQL_ASSOC);
	
	$pbBResult = mysql_query('SELECT * FROM  product_buy_bid WHERE product_buy_bid_id = "' . $pbBid . '"');
	$pbBRow = mysql_fetch_array($pbBResult, MYSQL_ASSOC);
	
	$pbOResult = mysql_query('SELECT * FROM  product_buy_offer WHERE product_buy_bid_id = "' . $pbBid . '" AND offer_type = "O"');
	
	$pbBResult2 = mysql_query('SELECT * FROM  product_buy_bid WHERE product_buy_bid_id = "' . $pbBid . '" AND member_id = "' . $mid . '" AND offer_type = "O"
		AND creation_time = (select max(creation_time) FROM product_buy_bid WHERE product_buy_id = "' . $pbid . '" AND member_id = "' . $mid . '")');

	if(mysql_num_rows($pbBResult2) > 0 && $pbRow["status"] != "F"){
?>
	<table id="tblBuyerOffer">
		<tr>
			<td><b>出價</b></td>
			<td>接受</td>
			<td>拒絕</td>
		</tr>
		<form id="frmCOffer" method="post" action="/bid/counteroffer">
			<tr>
				<td>
					HKD $<?php echo $pbBRow["price"];?>
				</td>
				<td><input name="radioBidPrice" class="radioBid" type="radio" value="accept"></td>
				<td><input name="radioBidPrice" class="radioBid" type="radio" value="reject"></td>
			</tr>
			<tr>
				<td colspan="3"><input id="txtCOfferPrice" name="cOfferPrice" type="text" style="display:none"/></td>
			</tr>
			<tr>
				<td colspan=“3"><b>條件</b></td>
			</tr>
			<?php 
			while ($pbORow = mysql_fetch_array($pbOResult, MYSQL_ASSOC)){
				$i++;
			?>
			<tr>
				<td><?php echo $i . '. ' . $pbORow["offer"];?></td>
				<td><input name="radioBid<?php echo $pbORow["product_buy_offer_id"]?>" class="radioBid" type="radio" value="accept"></td>
				<td><input name="radioBid<?php echo $pbORow["product_buy_offer_id"]?>" class="radioBid" type="radio" value="reject"></td>
			</tr>
			<tr>
				<td colspan="3">
					<input id="txtCOffer<?php echo $pbORow["product_buy_offer_id"];?>" 
						name="cOffer<?php echo $pbORow["product_buy_offer_id"]?>" type="text" style="display:none"/>
				</td>
			</tr>
			<?php 
			}
			?>
	
			<tr>
				<td colspan="3">
					<input id="btnCOffer" type="submit" value="還價" disabled="disabled"/>
					<input id="hidPbid" name="pbid" type="hidden" value="<?php echo $pbBRow["product_buy_id"];?>">
					<input id="hidPbBid" name="pbbid" type="hidden" value="<?php echo $pbBRow["product_buy_bid_id"];?>">
				</td>
			</tr>
		</form>
		<tr>
			<td colspan="3" class="center">
				<form id="frmAcceptOffer" method="post" action="/bid/buyeracceptoffer">
					<input id="btnBid" type="submit" value="成交" disabled="disabled"/>
					<input id="hidPbid" name="pbid" type="hidden" value="<?php echo $pbBRow["product_buy_id"];?>">
					<input id="hidPbBid" name="pbbid" type="hidden" value="<?php echo $pbBRow["product_buy_bid_id"];?>"> 
					<input id="hidOfferCounter" name="offerCounter" type="hidden" value="3">
				</form>
			</td>
		</tr>
	</table>
<?php
	}
	else{
		$sql = 'SELECT * FROM  product_buy_offer where product_buy_bid_id = "' . $pbBid . '"';
		$result = mysql_query($sql);
?>
	<table id="tblBuyerOffer">
		<tr>
			<td colspan="2"><b>出價</b></td>
		</tr>
		<tr>
			<td>
				HKD $<?php echo $pbBRow["price"];?>
			</td>
		</tr>
		<tr>
			<td colspan="2"><b>條件</b></td>
		</tr>
		<?php 
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$i++;
			echo '<tr>';
			echo '<td>';
			echo $i . ". " . $row["offer"];
			echo '</td>';
			echo '</tr>';
		}
		?>
	</table>
	<input id="hidPbid" name="pbid" type="hidden" value="<?php echo $pbBRow["product_buy_id"];?>">
	<input id="hidOfferCounter" name="offerCounter" type="hidden" value="3">
<?php 		
	} 
?>